package cn.laowen.org.controller;

import cn.laowen.basic.util.AjaxResult;
import cn.laowen.basic.util.PageList;
import cn.laowen.org.domain.Employee;
import cn.laowen.org.query.EmployeeQuery;
import cn.laowen.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeServiceImpl;

    //查询一条记录
    @GetMapping("/{id}")
    //方法
    @ApiOperation(value = "通过ID查询" )
    public Employee queryById(@PathVariable("id") long id){
        return employeeServiceImpl.queryById(id);
    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Employee> queryAll(Employee employee){
        return employeeServiceImpl.queryAll();
    }

    //高级查询 + 分页
    @PostMapping
    @ApiOperation(value = "高级查询 + 分页" )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeServiceImpl.queryPage(employeeQuery);
    }


    //新增或修改
    @PutMapping
    @ApiOperation(value = "新增或修改" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()!=null){
                employeeServiceImpl.update(employee);
            } else {
                employeeServiceImpl.save(employee);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me();
        }
    }
    //删除一条记录
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一条记录" )
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeServiceImpl.removeById(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("耶,原来是系统瓦了,正在殴打程序员!!!");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            employeeServiceImpl.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("耶,原来是系统瓦了,正在殴打程序员!!!");
        }
    }

}
